﻿using Cinemachine;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class AddCount : CallableActionNodeBase
    {
        protected override void OnRegisterPorts(FlowNode node)
        {
            var o = node.AddFlowOutput("In");
            var p1 = node.AddValueInput<string>("counterName");
            var p2 = node.AddValueInput<int>("addValue");
            
            node.AddFlowInput("Out", (Flow f) => { Invoke(p1.value, p2.value); o.Call(f); });

            node.AddValueOutput("count", (() =>
            {
                BlueprintCounter counter = BlueprintUtils.TryGetBlueprintCounter(p1.value);
                return counter.Count;
            }));
        }

        private void Invoke(string counterName, int addValue)
        {
            counterName = counterName.Trim();
            
            BlueprintCounter counter = BlueprintUtils.TryGetBlueprintCounter(counterName);
            counter.AddCount(addValue);
            CounterEvent.OnCounterValueChanged?.Invoke(counterName,counter.Count);
        }
    }
}